package com.kdgc.power.base.service;

import com.kdgc.power.base.mapper.BaseRegionMapper;
import com.kdgc.power.fw.bean.TreeNode;
import com.kdgc.power.fw.service.BaseService;
import org.springframework.stereotype.Service;
import com.kdgc.power.base.entity.BaseRegion;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BaseRegionService extends BaseService<BaseRegion> {
    @Resource
    private BaseRegionMapper baseRegionMapper;

    public List<TreeNode> findAll() {
        return baseRegionMapper.getTreeList();
    }

    public List<TreeNode> buildTree(String parentCode, List<TreeNode> dataList) {
        List<TreeNode> parents = dataList.stream().filter(d ->
                parentCode.equals(d.getPCode())).collect(Collectors.toList()
        );
        for (TreeNode parent : parents) {
            parent.setChildren(buildTree(parent.getCode(), dataList));
        }
        return parents;
    }

    public List<TreeNode> getTree() {
        List<TreeNode> all = findAll();
        return buildTree("-1", all);
    }

    public List<TreeNode> findByParentId(String parentId) {
     return baseRegionMapper.findByParentId(parentId);
    }
}